<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################

require "header.php";

?>
<p class="small" align="left">
Campaigns > Manage Multiple Campaigns
</p>
<p class="heading">
Manage Multiple Campaigns
</p><p>
With this tool you can edit, delete or reset the hits of some or all of your campaigns. To get started, choose a filter and click display campaigns.  Some browsers may have trouble displaying more than 50 campaigns, so it is a good idea to select the appropriate filter to minimize the amount of campaigns being displayed at one time.
</p>
<p>
 To make the necessary modifications, click on the action checkmark and make the change, then select the action you would like to perform below and click Save Changes.
</p>
<?
/*
//sets starting values if loading page from menu
if (!$filter_group && !$filter_status && !$filter_purpose) //clicking on stats in menu
{
	$starting_pref = mysql_fetch_row(mysql_query("select purpose,group1,status from adtrackz_preferences"));
	$filter_purpose = $starting_pref[0];
	$filter_group = $starting_pref[1];
	$filter_status = $starting_pref[2];
	$submit_top = "yes";
}
else //button was clicked, so see if group is default
{
	if (!$filter_group)
		$filter_group = "default";
}
*/
?>

<form action="masscampaign.php" method="post">
<!--MAIN CONTROLS-->
<table width="680" border="0" cellpadding="0" cellspacing="0" align="center" valign="center">
	<tr>
		<td>Purpose</td>
		<td>Group</td>
		<td>Status</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>
			<?
				if ($filter_purpose == "all purposes")
					$all_purposes_selected = "selected";
				elseif ($filter_purpose == "clicks")
					$clicks_selected = "selected";
				elseif ($filter_purpose == "actions")
					$actions_selected = "selected";
				elseif ($filter_purpose == "sales")
					$sales_selected = "selected";
				elseif ($filter_purpose == "all")
					$all_selected = "selected";
				elseif ($filter_purpose == "affiliate")
					$affiliate_selected = "selected";
			?>
			<select name="filter_purpose" class="small">
			<option value="all purposes" <?=$all_purposes_selected?>>All</option>
			<option value="clicks" <?=$clicks_selected?>>Clicks Only</option>
			<option value="actions" <?=$actions_selected?>>Clicks & Actions</option>
			<option value="sales" <?=$sales_selected?>>Clicks & Sales</option>
			<option value="all" <?=$all_selected?>>Clicks, Actions & Sales</option>
			<option value="affiliate" <?=$affiliate_selected?>>Affiliate Link</option>
			</select>
		</td>
		<td>
			<select name="filter_group" class="small">
			<?
				if ($filter_group == "all")
					echo "<option value=all selected>All</option>";
				else
					echo "<option value=all>All</option>";
				if ($filter_group == "default")
					echo "<option value=default selected>- default -</option>";
				else
					echo "<option value=default>- default -</option>";
				$groups = mysql_query("select * from adtrackz_groups order by groupname asc");
				while($group1 = mysql_fetch_row($groups))
				{
					if ($filter_group == $group1[0])
						echo "<option value=$group1[0] selected>$group1[1]</option>";
					else
						echo "<option value=$group1[0]>$group1[1]</option>";
				}
			?>
			</select>
		</td>
		<td>
			<?
				if ($filter_status == "all")
					$s_all_selected = "selected";
				elseif ($filter_status == "active")
					$s_active_selected = "selected";
				elseif ($filter_status == "ended")
					$s_ended_selected = "selected";
				elseif ($filter_status == "pending")
					$s_pending_selected = "selected";
			?>
			<select name="filter_status" class="small">
			<option value="all" <?=$s_all_selected?>>All</option>
			<option value="pending" <?=$s_pending_selected?>>pending</option>
			<option value="active" <?=$s_active_selected?>>active</option>
			<option value="ended" <?=$s_ended_selected?>>ended</option>
			</select> 
		</td>
		<td>
			<INPUT TYPE="submit" name="submit_top" value="Display Campaigns" height="12" width="15" class="small">
		</td>
	</tr>
</table>
<br>

<?
//if bottom pulldown was selected
if ($submit_bottom)
{
	if (!$checked_campaign && $confirm != "yes")
	{
		alert("no campaigns were selected");	
		echo "<br><br>";
	}
	elseif ($action == "edit") //make changes
	{
		for ($i=0;$i<count($checked_campaign);$i++) //for each checked campaign...
		{
			for ($j=0;$j<count($cID);$j++) //...go through all campaigns listed and...
			{
				//...look for the checked subcampaign...
				if ($cID[$j] == $checked_campaign[$i]) //$j is is the subcampaign to change
				{
					//check variables
					if (strlen($adcode[$j]) > 100)
						error("Ad code $adcode[$j] must be less than 100 characters.");
					elseif (!$adcode[$j])
						error("ad code is a required field.");
					elseif (preg_match("/(\W+|\s+)/", $adcode[$j], $matches))
						error("Ad code $adcode[$j] must contain letters,digits or and underscore character only.");
					elseif (strlen($url[$j]) > 255)
						error("Url $url[$j] must be less than 255 characters.");
					elseif (!$url[$j])
						error("Url is a required field for $adcode[$j].");
					elseif (!preg_match("/(http|https):\/\//", $url[$j]))
						error("Url $url[$j] is not valid.");
					elseif (!preg_match("/\b\d+\b/", $cost[$j]))
						error("Cost $cost[$j] must be in numerical format.");
					else
					{
						if ($ppc[$j] == "yes")
						{
							//find out if it's a reccuring cost first, if it is, take out 
							//those values so it's not recurring AND ppc at the same time
							$recurdata = mysql_fetch_row(mysql_query("select recurdays, repeat from adtrackz_campaigns where ID='$cID[$j]'"));
							if ($recurdata[0] && $recurdata[1])
							{
								mysql_query("update adtrackz_campaigns set recurdays='0', repeat='0' where ID='$cID[$j]'");
								echo mysql_error();
								echo "inrecur";
							}
							$ppc_save = 1;
						}
						else
							$ppc_save = 0;
						mysql_query("update adtrackz_campaigns set groupID='$group[$j]', adcode='$adcode[$j]', url='$url[$j]', cost='$cost[$j]', description='$description[$j]', purpose='$purpose[$j]', ppc='$ppc_save' where ID='$cID[$j]'");
						
						echo "<br>";											
						alert ("$adcode[$j] has been updated successfully.");
						echo "<br>";

						unset ($ppc_save);
						break;			
					}
				}
			}
		}
	}	
	elseif ($action == "delete") //delete the campaign
	{
		if (!$confirm)
		{
			for ($i=0;$i<count($checked_campaign);$i++) //for each checked campaign...
			{
				for ($j=0;$j<count($cID);$j++) //...go through all campaigns listed and...
				{
					//...look for the checked campaign...
					if ($cID[$j] == $checked_campaign[$i]) //$j is is the subcampaign to change
					{
						//build list of subcampaigns to delete
						$campaigns_2delete_display .= $adcode[$j].", ";
						$campaigns_2delete_form .= "<input type=hidden name=cID[] value=".$cID[$j].">";
						break;			
					}
				}
			}
			alert ("<p>Are you sure you want to remove the following campaigns:<b>$campaigns_2delete_display</b>? All existing data for these campaigns will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action="masscampaign.php" method=post>
					<input type=submit name=submit_bottom value=" Yes ">
					<?=$campaigns_2delete_form?>
					<input type=hidden name=action value=delete>
					<input type=hidden name=confirm value=yes>
					</form>
				</td>
				<td>
					<form action="masscampaign.php" method=post>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{
			for ($i=0;$i<count($cID);$i++)
			{
				$campaign = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID[$i]'"));
				//what if campaign is in a split test group
				if (mysql_fetch_row(mysql_query("select * from adtrackz_split_test_campaigns where campaignID='$cID[$i]'")))
					alert("$campaign[0] can't be deleted because it's part of a split test. Delete $campaign[0] from the split test and try again.");
				else
				{
					mysql_query("delete from adtrackz_campaigns where ID='$cID[$i]'");
					mysql_query("delete from adtrackz_clicks where campaignID='$cID[$i]'");
					mysql_query("delete from adtrackz_actions where campaignID='$cID[$i]'");
					mysql_query("delete from adtrackz_sales where campaignID='$cID[$i]'");
					mysql_query("delete from adtrackz_campaigns_st where campaignID='$cID[$i]'");
					alert("$campaign[0] has been deleted.");
				}
			}
			require "footer.php";
			exit;
		}
	}
	elseif($action == "reset")
	{
		if (!$confirm)
		{
			for ($i=0;$i<count($checked_campaign);$i++) //for each checked campaign...
			{
				for ($j=0;$j<count($cID);$j++) //...go through all campaigns listed and...
				{
					//...look for the checked campaign...
					if ($cID[$j] == $checked_campaign[$i]) //$j is is the campaign to change
					{
						//build list of subcampaigns to delete
						$campaigns_2delete_display .= $adcode[$j].", ";
						$campaigns_2delete_form .= "<input type=hidden name=cID[] value=".$cID[$j].">";
						break;			
					}
				}
			}

			alert ("<p>Are you sure you want to remove the data from the following campaigns:<b>$campaigns_2delete_display</b>? All existing data for these campaigns will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action="masscampaign.php" method=post>
					<input type=submit name=submit_bottom value=" Yes ">
					<?=$campaigns_2delete_form?>
					<input type=hidden name=action value=reset>
					<input type=hidden name=confirm value=yes>
					</form>
				</td>
				<td>
					<form action="masscampaign.php" method=post>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{
			for ($i=0;$i<count($cID);$i++)
			{
				$campaign = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID[$i]'"));
				mysql_query("delete from adtrackz_clicks where campaignID='$cID[$i]'");
				mysql_query("delete from adtrackz_actions where campaignID='$cID[$i]'");
				mysql_query("delete from adtrackz_sales where campaignID='$cID[$i]'");
				alert("$campaign[0] has been reset.");
			}
			require "footer.php";
			exit;
		}
	}
}

//DISPLAY  TABLE
//STEP 1: FILTER BY PURPOSE, GROUP
// all dynamic campaigns have purpose == all, group=dynamic

if ($filter_purpose == "all purposes" && $filter_group == "all")
	$campaigns = mysql_query("select * from adtrackz_campaigns");
elseif ($filter_purpose == "all purposes" && $filter_group != "all")
{
	if ($filter_group == "default")
		$filter_group = 0;
	$campaigns = mysql_query("select * from adtrackz_campaigns where groupID = '$filter_group'");
}
elseif($filter_purpose != "all purposes" && $filter_group == "all")
	$campaigns = mysql_query("select * from adtrackz_campaigns where purpose = '$filter_purpose'");
else
{
	$campaigns = mysql_query("select * from adtrackz_campaigns where  groupID = '$filter_group' && purpose = '$filter_purpose'");
}

//STEP 2: FILTER BY STATUS
// all dynamic campaigns are constantly running, so "all"
$now = date("Y-m-d H:m:s");
if ($filter_status == "active")
{
	while($campaign1 = mysql_fetch_row($campaigns))
	{
		//if now is in between startdate and enddate
		if (earlier_date($now,$campaign1[5]) == $campaign1[5] && earlier_date($now,$campaign1[6]) == $now)
			$campaignIDs[$campaign1[0]]=$campaign1[2];
	}
}
elseif ($filter_status == "ended")
{
	while($campaign1 = mysql_fetch_row($campaigns))
	{
		// enddate is before now (campaign has ended)
		if (earlier_date($now,$campaign1[6]) == $campaign1[6])
			$campaignIDs[$campaign1[0]]=$campaign1[2];
	}
}
elseif ($filter_status == "pending")
{
	while($campaign1 = mysql_fetch_row($campaigns))
	{
		//now is before startdate (campaign hasn't started yet)
		if (earlier_date($now,$campaign1[5]) == $now)
			$campaignIDs[$campaign1[0]]=$campaign1[2];
	}
}
elseif ($filter_status == "all")
{
	while($campaign1 = mysql_fetch_row($campaigns))
		$campaignIDs[$campaign1[0]]=$campaign1[2];
}

//sort array
if ($campaignIDs)
	asort($campaignIDs);
//$campaignIDs contains the adcodes of each campaign that fits the filter
//the filter is the same as the preferences from stats

if (!$campaignIDs)
{
	if (!$filter_group && !$filter_status && !$filter_purpose) //clicking on stats in menu
		alert("Please choose a filter setting and click Display Campaigns.");
	else
		alert("There are no campaigns to display for this filter setting.");
}
else
{
	?>
	<!--TABLE HEADER-->
	<form action="masscampaign.php" method=post>
	<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
	<tr>
		<td class="tbhead1">Campaign</td>
		<td class="tbhead1">Url</td>
		<td class="tbhead1">Group</td>
		<td class="tbhead1">Purpose</td>
		<td class="tbhead1">Cost</td>
		<td class="tbhead1">PPC</td>
		<td class="tbhead1">Description</td>
		<td class="tbhead1">Action</td>
	</tr>
	<?

	//DISPLAY TABLE BODY
	foreach ($campaignIDs as $adcode)
	{
		$campaigndata = mysql_fetch_row(mysql_query("select * from adtrackz_campaigns where adcode='$adcode'"));
		if ($campaigndata[11])
		{
			$ppc_yes = "selected";
		}
		else
			$ppc_no = "selected";
		$purpose = $campaigndata[8];
		if ($campaigndata[0])
		{
	?>
	<tr>
		<td class="tb1" valign=middle>
			<input type=hidden name="cID[]" value=<?=$campaigndata[0]?>>
			<input type=input name="adcode[]" size=10 value=<?=$campaigndata[2]?>>
		</td>
		<td class="tb1" valign=middle><input type=input name="url[]" value=<?=$campaigndata[3]?>></td>	
		<td class="tb1" valign=middle>			
		<select name="group[]">		
		<?
		$groups = mysql_query("select * from adtrackz_groups order by groupname asc");
		if(!$campaigndata[1])
		{
			echo "<option value=\"0\" selected>- default -</option>";
			while($group1 = mysql_fetch_row($groups))
					echo "<option value=\"$group1[0]\">$group1[1]</option>";
		}
		else
		{
			echo "<option value=\"0\">- default -</option>";
			while($group1 = mysql_fetch_row($groups))
			{
				if ($campaigndata[1] == $group1[0])
					echo "<option value=\"$group1[0]\" selected>$group1[1]</option>";
				else
					echo "<option value=\"$group1[0]\">$group1[1]</option>";
			}
		}
		?>
		</select>
		</td>
		<td valign=middle>			
		<?
				if (!$purpose || $purpose == "clicks")
					$clicks_s = "selected";
				elseif ($purpose == "actions")
					$actions_s = "selected";
				elseif ($purpose == "sales")
					$sales_s = "selected";
				elseif ($purpose == "all")
					$all_s = "selected";
				elseif ($purpose == "affiliate")
					$affiliate_s = "selected";
			?>
			<select name="purpose[]">
			<option value="clicks" <?=$clicks_s?>>Clicks Only</option>
			<option value="actions" <?=$actions_s?>>Clicks & Actions</option>
			<option value="sales" <?=$sales_s?>>Clicks & Sales</option>
			<option value="all" <?=$all_s?>>Clicks, Actions & Sales</option>
			<option value="affiliate" <?=$affiliate_s?>>Affiliate Link</option>
			</select> 
		</td>
		<td class="tb1" valign=middle><input type=input name="cost[]" size=3 value=<?=$campaigndata[4]?>></td>	
		<td class="tb1" valign=middle>
			<select name="ppc[]">
			<option value="yes" <?=$ppc_yes?>>Yes</option>
			<option value="no" <?=$ppc_no?>>No</option>
			</select> 
		</td>	
		<td class="tb1" valign=middle><textarea name="description[]" cols=12 rows=2><?=$campaigndata[7]?></textarea></td>	
		<td class="tb1" valign=middle><input type=checkbox name="checked_campaign[]" value="<?=$campaigndata[0]?>"></td>
	</tr>
	<?
			unset ($ppc_yes, $ppc_no, $click_s, $actions_s, $sales_s, $all_s, $affiliate_s);
		}
	}
}
?>
</table>
<br><br>
<center>
With Selected Campaigns: &nbsp;
<select name="action">
<option value="edit">Edit Campaigns</option>
<option value="reset">Reset Hits</option>
<option value="delete">Delete Campaigns</option>
</select>
<input type=submit name="submit_bottom" value="Make Changes">
</center>

</form>
<?

require "footer.php";
?>